﻿using System;
using BrainTechLLC.Silverlight.InputControls;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditGameCharTemplate : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { GameCharTemplateObject = o as GameCharTemplate; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(GameCharTemplate); } }
        public string VisualizerName { get { return "EditGameCharTemplate"; } }

        public static readonly DependencyProperty GameCharTemplateObjectProperty = DependencyProperty.Register(
            "GameCharTemplateObject", typeof(GameCharTemplate), typeof(EditGameCharTemplate), new PropertyMetadata(null, new PropertyChangedCallback(GameCharTemplateChanged)));

        public GameCharTemplate GameCharTemplateObject
        {
            get { return (GameCharTemplate)GetValue(GameCharTemplateObjectProperty); }
            set { SetValue(GameCharTemplateObjectProperty, value); }
        }

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditGameCharTemplate, T2> action)
        {
            d.HandleChange<EditGameCharTemplate, T2>(e, action, (t) => t.UpdateBindings());
        }


        private static void GameCharTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<GameCharTemplate>(d, e, (t, val) => t.GameCharTemplateObject = val);
        }

        public override void UpdateBindings()
        {
            if (gridData != null)
                gridData.DataContext = GameCharTemplateObject;
        }

        public override string TitleText
        {
            get
            {
                if (GameCharTemplateObject == null)
                    return string.Empty;

                return string.Format("GameChar Template {0} ({1} Props)", GameCharTemplateObject.FriendlyName, GameCharTemplateObject.PropertiesCount);
            }
        }

        public EditGameCharTemplate()
        {
            InitializeComponent();
            txtAddAbilityTemplate.AttachFocusWatcher();
        }

        private void basicTemplate_Loaded(object sender, RoutedEventArgs e)
        {
            //editBasicTemplate.wc.Indent = wc.Indent + 1;
        }

        private void txtAddAbilityTemplate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;

                string temp = txtAddAbilityTemplate.Text;

                if (!GameCharTemplateObject.AbilityTemplateNames.ContainsValue(temp))
                    GameCharTemplateObject.AbilityTemplateNames.AddValue(temp);

                txtAddAbilityTemplate.Text = string.Empty;
            }
        }

        public override void OnSaveData()
        {
            DataAccess.Client.StoreGameCharTemplateAsync(DataAccess.AssignedUniqueId, GameCharTemplateObject);
        }

        private void btnCreateChar_Click(object sender, RoutedEventArgs e)
        {
            DataAccess.CreateCharCompleted += new EventHandler<ObjectEventArgsNonRef<long>>(DataAccess_CreateCharCompleted);
            DataAccess.Client.CreateCharFromTemplateAsync(DataAccess.AssignedUniqueId, GameCharTemplateObject.AssociatedID, DataAccess.CurrentlyPlaying.Location);
        }

        void DataAccess_CreateCharCompleted(object sender, ObjectEventArgsNonRef<long> e)
        {
            DataAccess.CreateCharCompleted -= DataAccess_CreateCharCompleted;
        }

    }
}
